package com.example.yk2021xm04.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yk2021xm04.mapper.TodoMapper;
import com.example.yk2021xm04.pojo.Todo;
import com.example.yk2021xm04.pojo.User;
import com.example.yk2021xm04.query.TodoQuery;
import com.example.yk2021xm04.service.ITodoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yk2021xm04.utils.AssertUtil;
import com.example.yk2021xm04.utils.PageResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZengQi
 * @since 2021-12-06
 */
@Service
public class TodoServiceImpl extends ServiceImpl<TodoMapper, Todo> implements ITodoService {

    @Override
    public Map<String, Object> todoList(Integer userId, TodoQuery Todo) {
        //分页的类
        IPage<Todo> page = new Page<Todo>(Todo.getPage(), Todo.getLimit());
        QueryWrapper<Todo> queryWrapper = new QueryWrapper<Todo>();

        queryWrapper.eq("creator_id",userId);

        //这里就是代表 sql语句中的 like
        if(StringUtils.isNotBlank(Todo.getTitle())) {
            queryWrapper.like("title",Todo.getTitle());
        }
        //已经写好的接口
        page = this.baseMapper.selectPage(page,queryWrapper);

        return PageResultUtil.getResult(page.getTotal(),page.getRecords());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteTodo(Integer[] ids) {
        AssertUtil.isTrue(null==ids || ids.length==0,"请选择待删除的id");
        AssertUtil.isTrue(!(this.removeByIds(Arrays.asList(ids))),"待办事项删除失败");
    }

    /**
     *
     * @author ZengQi
     * @date 2021/12/18 0018 15:31
     * @param todo
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addTodo(Todo todo) {
        /**
         * 标题、内容、日期范围、时间限制
         * 非空
         * 状态默认为0，未完成
         */
        AssertUtil.isTrue(StringUtils.isBlank(todo.getTitle()),"标题不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(todo.getContent()),"内容不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(todo.getBeginTime().toString()),"开始时间不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(todo.getEndTime().toString()),"结束时间不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(todo.getLimitTime()),"时间限制不能为空");
        todo.setCreateTime(LocalDate.now());
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        todo.setCreatorId(user.getId());
        todo.setCreator(user.getTrueName());
        todo.setStatus(0);
        AssertUtil.isTrue(!(this.save(todo)),"待办事项添加失败");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateTodo(Todo todo) {
        AssertUtil.isTrue(!(this.updateById(todo)),"待办事项更新失败");
    }

    @Override
    public void switchStatus(Integer id) {
        //根据id查询到当前待办事项状态
        //然后修改状态
        Todo todo = this.getById(id);
        AssertUtil.isTrue(null == todo, "无该待办事项");
        if(todo.getStatus() == 1) {
            todo.setStatus(0);
        } else if(todo.getStatus() == 0) {
            todo.setStatus(1);
        }
        AssertUtil.isTrue(!(this.updateById(todo)), "待办事项完成状态更新失败");
    }

    @Override
    public List<Object> createTimeByDay() {
        return this.baseMapper.createTimeByDay();
    }

    @Override
    public List<Object> createCountByDay() {
        return this.baseMapper.createCountByDay();
    }

    @Override
    public Integer TodoCountInDay() {
        return this.baseMapper.TodoCountInDay();
    }
}
